package vue;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.BorderFactory;
import javax.swing.JPanel;

import bean.CombinaisonBean;

import manager.ColorManager;


public class PanelCombinaison extends JPanel {
	
	private int position = 0;
	
	private ColorManager cm = new ColorManager();
	
	private Pion[] pions = new Pion[4];
	
	private Color[] couleurs = cm.getAllColors();
	
	private CombinaisonBean essaiCombi;

	public PanelCombinaison ()
	{
		super();
		
		essaiCombi = new CombinaisonBean();
		
		this.setLayout(new GridBagLayout());
		this.setPreferredSize(new Dimension(320,80));
		
		pions[0] = new Pion();
		pions[0].setBorder(BorderFactory.createLineBorder (Color.red, 2));
		pions[1] = new Pion();
		pions[1].setBorder(BorderFactory.createLineBorder (Color.black, 1));
		pions[2] = new Pion();
		pions[2].setBorder(BorderFactory.createLineBorder (Color.black, 1));
		pions[3] = new Pion();
		pions[3].setBorder(BorderFactory.createLineBorder (Color.black, 1));

		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = gbc.gridy = 0;
		gbc.anchor = GridBagConstraints.CENTER;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.weightx = 1;
		gbc.weighty = 1;
		this.add(pions[0], gbc);
		gbc.gridx = 1;
		gbc.gridy = 0;
		this.add(pions[1], gbc);
		gbc.gridx = 2;
		gbc.gridy = 0;
		this.add(pions[2], gbc);
		gbc.gridx = 3;
		gbc.gridy = 0;
		this.add(pions[3], gbc);
	}
	
	public void reinit()
	{
		for (int i=0; i < pions.length; i++)
		{
			this.pions[i].setColor(cm.getColor(-1));
			if (i == 0)
			{
				pions[i].setBorder(BorderFactory.createLineBorder (Color.red, 2));
			}
			else
			{
				pions[i].setBorder(BorderFactory.createLineBorder (Color.black, 1));
			}
			position = 0;
		}
		essaiCombi = new CombinaisonBean();
	}
	
	public void changerCouleurUp()
	{
		for (int i = 0; i < couleurs.length; i++)
		{
			if (i == couleurs.length -1)
			{
				pions[position].setColor(couleurs[0]);
				setColorCombi(essaiCombi,0);
			}
			else if (pions[position].getColor() == couleurs[i])
			{
				pions[position].setColor(couleurs[i+1]);
				setColorCombi(essaiCombi,i+1);
				i = couleurs.length;
			}
		}
	}
	
	public void changerCouleurDown()
	{
		for (int i = couleurs.length-1; i >= 0; i--)
		{
			if (i==0)
			{
				pions[position].setColor(couleurs[couleurs.length-1]);
				setColorCombi(essaiCombi,couleurs.length-1);
			}
			else if (pions[position].getColor() == couleurs[i])
			{
				pions[position].setColor(couleurs[i-1]);
				setColorCombi(essaiCombi,i-1);
				i = -1;
			}
		}
	}
	
	public void changerPositionRight()
	{
		if (position == 3)
		{
			pions[3].setBorder(BorderFactory.createLineBorder (Color.black, 1));
			pions[0].setBorder(BorderFactory.createLineBorder (Color.red, 2));
			position = 0;
		}
		else
		{
			pions[position].setBorder(BorderFactory.createLineBorder (Color.black, 1));
			pions[position + 1].setBorder(BorderFactory.createLineBorder (Color.red, 2));
			position = position + 1;
		}
	}
	
	public void changerPositionLeft()
	{
		if (position == 0)
		{
			pions[0].setBorder(BorderFactory.createLineBorder (Color.black, 1));
			pions[3].setBorder(BorderFactory.createLineBorder (Color.red, 2));
			position = 3;
		}
		else
		{
			pions[position].setBorder(BorderFactory.createLineBorder (Color.black, 1));
			pions[position - 1].setBorder(BorderFactory.createLineBorder (Color.red, 2));
			position = position - 1;
		}
	}
	
	public boolean estValide()
	{
		for (int i = 0; i< pions.length; i++)
		{
			if (pions[i].getColor().equals(cm.getColor(-1))) return false;
		}
		return true;
	}
	
	public Pion[] getPions()
	{
		return pions;
	}
	
	public void setColorCombi(CombinaisonBean c,int i)
	{
		switch (position)
		{
		case 0:
			c.setP1(i);
		case 1:
			c.setP2(i);
		case 2:
			c.setP3(i);
		case 3:
			c.setP4(i);
		}
	}
	
	public CombinaisonBean getEssaiCombi()
	{
		return essaiCombi;
	}
}
